﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TCDC_System.Register
{
    public partial class Register1 : System.Web.UI.Page
    {
        private string lang { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lang = Request.QueryString["lang"];

                /*** Create Session ***/
                SessionInit();

                LoadDDLType(lang);
                LoadDDLGroup(lang);
                LoadDDLCampaign(lang, "", "");

                SessionValue();
            }
        }

        private void SessionInit()
        {
            if (Session["campaign_type"] == null)
            {
                Session["campaign_type"] = "";
            }
            if (Session["campaign_group"] == null)
            {
                Session["campaign_group"] = "";
            }
            if (Session["campaign"] == null)
            {
                Session["campaign"] = "";
            }
            if (Session["email"] == null)
            {
                Session["email"] = "";
            }
            if (Session["password"] == null)
            {
                Session["password"] = "";
            }
            if (Session["confirm"] == null)
            {
                Session["confirm"] = "";
            }
            
        }

        private void SessionValue()
        {
            dlType.SelectedValue = Session["campaign_type"].ToString();
            dlGroup.SelectedValue = Session["campaign_group"].ToString();

            LoadDDLCampaign(lang, Session["campaign_type"].ToString(), Session["campaign_group"].ToString());

            dlCampaign.SelectedValue = Session["campaign"].ToString();
            txtEmail.Text = Session["email"].ToString();
            txtPassword.Text = Session["password"].ToString();
            txtConfirm.Text = Session["Confirm"].ToString();
        }

        private void LoadDDLType(string lang)
        {
            try
            {
                string host = HttpContext.Current.Request.Url.Authority;

                DataSet ds = new DataSet();
                ds.ReadXml("http://" + host + "/Attribute/xml/MemberType.ashx");

                dlType.DataSource = ds;
                string DataText = (lang == "en") ? "type_name_en" : "type_name_th";

                dlType.DataTextField = DataText;
                dlType.DataValueField = "type_id";
                dlType.DataBind();

                string DefaultVal = (lang == "en") ? "-- Select --" : "-- เลือกรายการ --";
                dlType.Items.Insert(0, new ListItem(DefaultVal, ""));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoadDDLGroup(string lang)
        {
            try
            {
                string host = HttpContext.Current.Request.Url.Authority;

                DataSet ds = new DataSet();
                ds.ReadXml("http://" + host + "/Attribute/xml/CampaignGroup.ashx");

                dlGroup.DataSource = ds;
                string DataText = (lang == "en") ? "group_name_en" : "group_name_th";

                dlGroup.DataTextField = DataText;
                dlGroup.DataValueField = "group_id";
                dlGroup.DataBind();

                string DefaultVal = (lang == "en") ? "-- Select --" : "-- เลือกรายการ --";
                dlGroup.Items.Insert(0, new ListItem(DefaultVal, ""));

            }

            catch (Exception ex)
            {

                throw ex;

            }
        }

        private void LoadDDLCampaign(string lang, string type, string group)
        {
            try
            {
                string host = HttpContext.Current.Request.Url.Authority;

                DataSet ds = new DataSet();
                ds.ReadXml("http://" + host + "/Attribute/xml/Campaign.ashx?type=" + type + "&group=" + group);

                dlCampaign.DataSource = ds;
                string DataText = (lang == "en") ? "campaign_name_en" : "campaign_name_th";

                dlCampaign.DataTextField = "display_value_th";
                dlCampaign.DataValueField = "campaign_id";
                dlCampaign.DataBind();

                string DefaultVal = (lang == "en") ? "-- Select --" : "-- เลือกรายการ --";
                dlCampaign.Items.Insert(0, new ListItem(DefaultVal, ""));

            }

            catch (Exception ex)
            {

                throw ex;

            }
        }

        protected void dlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = dlType.Text;
            string group = dlGroup.Text;

            LoadDDLCampaign(this.lang, type, group);
        }

        protected void dlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = dlType.Text;
            string group = dlGroup.Text;

            LoadDDLCampaign(this.lang, type, group);
        }

        protected void btnNext_Click(object sender, ImageClickEventArgs e)
        {
            Session["campaign_type"] = dlType.Text;
            Session["campaign_group"] = dlGroup.Text;
            Session["campaign"] = dlCampaign.Text;
            Session["email"] = txtEmail.Text;
            Session["password"] = txtPassword.Text;
            Session["confirm"] = txtConfirm.Text;

            Response.Redirect("Register2.aspx");
        }

 


    }
}